package com.ruoyi.common.core.redis;

import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class MyCache implements Cache {
    //存储数据
    private Map<String,Object> storage = new ConcurrentHashMap<>();
    @Override
    public String getName() {
        return null;
    }

    @Override
    public Object getNativeCache() {
        return null;
    }
    public boolean hasKey(String key){
        return storage.containsKey(key);
    }
    @Override
    public ValueWrapper get(Object key) {
        String k = key.toString();
        Object value = storage.get(k);
        return Objects.isNull(value) ? null : new SimpleValueWrapper(value);
    }

    @Override
    public <T> T get(Object key, Class<T> type) {
        return null;
    }

    @Override
    public <T> T get(Object key, Callable<T> valueLoader) {
        return null;
    }

    @Override
    public void put(Object key, Object value) {
        if (Objects.isNull(value)){
            return;
        }
        storage.put(key.toString(),value);
    }

    @Override
    public void evict(Object key) {
        storage.remove(key.toString());
    }
    // 删除集合
    public boolean deleteObject(final Collection collection){
        collection.forEach(o -> {
            storage.remove(o.toString());
        } );
        return true;
    }

    // 获取所有的keys
    public Collection<String> keys(final String pattern){
        return storage.keySet();
    }

    @Override
    public void clear() {

    }
}
